package exception

// ErrorCode 错误码定义
type ErrorCode int

const (
	// ErrUnknown ===== General Errors =====
	ErrUnknown ErrorCode = iota
	ErrInternal
	ErrInvalidArgument
	ErrNotFound
	ErrAlreadyExists
	ErrPermissionDenied
	ErrResourceExhausted
	ErrFailedPrecondition
	ErrAborted
	ErrOutOfRange
	ErrUnimplemented
	ErrUnavailable
	ErrDeadlineExceeded
	ErrCancelled

	// ErrConfigNotFound ===== Config Errors =====
	ErrConfigNotFound
	ErrConfigInvalid
	ErrConfigLoadFailed
	ErrConfigValidationFailed

	// ErrTaskIDIsNil task-id 为空
	ErrTaskIDIsNil
	// ErrTaskNotFound ===== Task Errors =====
	ErrTaskNotFound
	ErrTaskAlreadyRunning
	ErrTaskExecutionFailed
	ErrTaskTimeoutExceeded
	ErrTaskCancelled

	// ErrCollectorNotFound ===== Collector Errors =====
	ErrCollectorNotFound
	ErrTaskStatusFileNotFound
	ErrCollectorInitFailed
	ErrCollectorExecutionFailed

	// ErrFileNotFound ===== File Errors =====
	ErrFileNotFound
	ErrFilePermissionDenied
	ErrFileReadFailed
	ErrFileWriteFailed

	// ErrNetworkUnavailable ===== Network Errors =====
	ErrNetworkUnavailable
	ErrNetworkTimeout
	ErrNetworkConnectionFailed

	// ErrSystemInfoUnavailable 系统信息不可用
	ErrSystemInfoUnavailable
	ErrSystemResourceUnavailable
	ErrSystemPermissionDenied
)

// Map for ErrorCode string representation
var errorCodeMap = map[ErrorCode]string{
	ErrUnknown:                   "UNKNOWN",
	ErrInternal:                  "INTERNAL",
	ErrInvalidArgument:           "INVALID_ARGUMENT",
	ErrNotFound:                  "NOT_FOUND",
	ErrAlreadyExists:             "ALREADY_EXISTS",
	ErrPermissionDenied:          "PERMISSION_DENIED",
	ErrResourceExhausted:         "RESOURCE_EXHAUSTED",
	ErrFailedPrecondition:        "FAILED_PRECONDITION",
	ErrAborted:                   "ABORTED",
	ErrOutOfRange:                "OUT_OF_RANGE",
	ErrUnimplemented:             "UNIMPLEMENTED",
	ErrUnavailable:               "UNAVAILABLE",
	ErrDeadlineExceeded:          "DEADLINE_EXCEEDED",
	ErrCancelled:                 "CANCELLED",
	ErrConfigNotFound:            "CONFIG_NOT_FOUND",
	ErrConfigInvalid:             "CONFIG_INVALID",
	ErrConfigLoadFailed:          "CONFIG_LOAD_FAILED",
	ErrConfigValidationFailed:    "CONFIG_VALIDATION_FAILED",
	ErrTaskIDIsNil:               "TASK_ID_IS_NIL",
	ErrTaskNotFound:              "TASK_NOT_FOUND",
	ErrTaskStatusFileNotFound:    "TASK_STATUS_FILE_NOT_FOUND",
	ErrTaskAlreadyRunning:        "TASK_ALREADY_RUNNING",
	ErrTaskExecutionFailed:       "TASK_EXECUTION_FAILED",
	ErrTaskTimeoutExceeded:       "TASK_TIMEOUT_EXCEEDED",
	ErrTaskCancelled:             "TASK_CANCELLED",
	ErrCollectorNotFound:         "COLLECTOR_NOT_FOUND",
	ErrCollectorInitFailed:       "COLLECTOR_INIT_FAILED",
	ErrCollectorExecutionFailed:  "COLLECTOR_EXECUTION_FAILED",
	ErrFileNotFound:              "FILE_NOT_FOUND",
	ErrFilePermissionDenied:      "FILE_PERMISSION_DENIED",
	ErrFileReadFailed:            "FILE_READ_FAILED",
	ErrFileWriteFailed:           "FILE_WRITE_FAILED",
	ErrNetworkUnavailable:        "NETWORK_UNAVAILABLE",
	ErrNetworkTimeout:            "NETWORK_TIMEOUT",
	ErrNetworkConnectionFailed:   "NETWORK_CONNECTION_FAILED",
	ErrSystemInfoUnavailable:     "SYSTEM_INFO_UNAVAILABLE",
	ErrSystemResourceUnavailable: "SYSTEM_RESOURCE_UNAVAILABLE",
	ErrSystemPermissionDenied:    "SYSTEM_PERMISSION_DENIED",
}

// String 返回错误代码的字符串表示
func (c ErrorCode) String() string {
	if str, ok := errorCodeMap[c]; ok {
		return str
	}
	return "UNKNOWN"
}
